//
//  InviteInformationVCViewController.m
//  FindJob
//
//  Created by 233 on 16/2/29.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "InviteInformationVCViewController.h"
#import "RecruitmentAPIManger.h"
#import "CustomTableViewCell.h"
#import "UserModel.h"
#import <MJRefresh.h>

static NSString *const kPositionDetailVC = @"PositionDetailVC";

@interface InviteInformationVCViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,copy)NSArray *dataSource;
@property(nonatomic,strong)RecruitmentAPIManger *recruitmentAPIManger;
- (void)initializeDataSource;/**< 初始化数据源 */
- (void)initializeUserIntefcace;  /**< 初始化用户界面 */
@end

@implementation InviteInformationVCViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.view.window.layer removeAllAnimations];
    //转场动画
    CATransition *transition = [CATransition new];
    //持续时间
    transition.duration = 0.7f;
    //线性规律
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    transition.type = @"pageCurl";
    transition.subtype = kCATransitionFromLeft;
    
    //添加动画
    [self.view.window.layer addAnimation:transition forKey:Nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeUserIntefcace];
}
- (void)initializeDataSource{
    _dataSource = [[NSArray alloc] init];
    UserModel *userModel = [UserModel shareUserModel];
    if (userModel.isSelcetedTable) {
        _dataSource = self.infoArr;
    }else{
        
        int page = 1;
        NSNumber *num = [NSNumber numberWithInt:page];
        NSDictionary *parameter = @{@"sortname":@"0",@"page":num};
        [self.recruitmentAPIManger startRecruitmentInfoWithParameters:parameter responds:^(RespondsStatus status, NSString *message, id resultData) {
        if(status == RespondsStatusSuccess){
            self.dataSource = resultData;
            [self.tableView reloadData];
        }
        NSLog(@"message = %@",message);
    }];
 }
    
    
    _tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [_tableView.mj_header endRefreshing];
        int page = 1;
        NSNumber *num = [NSNumber numberWithInt:page];
        NSDictionary *parameter = @{@"sortname":@"0",@"page":num};
        [self.recruitmentAPIManger startRecruitmentInfoWithParameters:parameter responds:^(RespondsStatus status, NSString *message, id resultData) {
            if(status == RespondsStatusSuccess){
                self.dataSource = resultData;
                [self.tableView reloadData];
            }
            NSLog(@"message = %@",message);
        }];
    }];
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    _tableView.mj_header.automaticallyChangeAlpha = YES;
    

}
- (void)initializeUserIntefcace{
    UINib *customNib = [UINib nibWithNibName: NSStringFromClass([CustomTableViewCell class]) bundle:nil];
    [self.tableView registerNib:customNib forCellReuseIdentifier:kResusableCellIdientifier];
    
    
}
#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   return  _dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kResusableCellIdientifier];
    cell.position.text = _dataSource[indexPath.row][@"position"];
    cell.salaryLabel.text = _dataSource[indexPath.row][@"salary"];
    cell.companyLabel.text = _dataSource[indexPath.row][@"company"];
    NSString *timeStr = _dataSource[indexPath.row][@"releaseTime"];
    long long time = [timeStr longLongValue];
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:time / 1000.0];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *newTimeStr = [dateFormatter stringFromDate:date];
    cell.timerLabel.text = newTimeStr;
    
    return cell;
    
}
#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    [self.view.window.layer removeAllAnimations];
    //转场动画
    CATransition *transition = [CATransition new];
    //持续时间
    transition.duration = 0.7f;
    //线性规律
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    transition.type = @"cube";
    transition.subtype = kCATransitionFromLeft;
    
    //添加动画
    [self.view.window.layer addAnimation:transition forKey:Nil];

    [UserModel shareUserModel].selectedDict = _dataSource[indexPath.row];
    [self performSegueWithIdentifier:kPositionDetailVC sender:self];
    
}

#pragma mark *** UIbutton action ***
- (IBAction)unwindToMainViewController:(UIStoryboardSegue *)unwindSegue{
    NSLog(@"================");
    [self.view.window.layer removeAllAnimations];
    //转场动画
    CATransition *transition = [CATransition new];
    //持续时间
    transition.duration = 0.7f;
    //线性规律
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    transition.type = @"cube";
    transition.subtype = kCATransitionFromLeft;
    
    //添加动画
    [self.view.window.layer addAnimation:transition forKey:Nil];
}
#pragma mark -- getters
- (RecruitmentAPIManger *)recruitmentAPIManger{
    if (!_recruitmentAPIManger) {
        _recruitmentAPIManger = [[RecruitmentAPIManger alloc] init];
        
    }
    return _recruitmentAPIManger;
}
- (NSArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [[NSArray alloc] init];
    
    }
    return _dataSource;
}
@end
